//
//  ZYVisitorView.swift
//  weibo-Swift
//
//  Created by Apple on 16/7/19.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

//代理协议
protocol VisitorViewDelegate:NSObjectProtocol {
     
    func  registerClick()
    
    func loginClick()
}




//访客视图
class ZYVisitorView: UIView {
//    定义代理
    weak var Delegate: VisitorViewDelegate?
    
//   重写了init方法就必须重写这个方法。下面那个是代码创建会走的方法。这个是storyboard创建会走的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    
//    重写init给访客视图添加控件和图片
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 237 / 256.0, alpha: 1)
        
        
        self.setupUI()
        
    }

   
    
    
/// 转轮开始旋转
    func startAnimation(){
//     使用核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置参数
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
//        当切换到其他控制器系统会将核心动画结束，一结束默认就会将核心动画移除
        anim.removedOnCompletion = false
//        将核心动画添加到图层上面
        iconview.layer.addAnimation(anim, forKey: nil)
        
    }
    
   
    
    
func setupVistorInfo(message: String , imageName: String) {
    houseview.hidden = true
    self.sendSubviewToBack(coverview)
    iconview.image = UIImage(named: imageName)
    messagelabel.text = message
    
     }
    
    private func setupUI(){
        //        添加子控件
        self.addSubview(iconview)
        self.addSubview(coverview)
        self.addSubview(houseview)
        self.addSubview(messagelabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        //        设置约束
        //        使用纯代码约束需要关闭autoresizing的影响
        iconview.translatesAutoresizingMaskIntoConstraints = false
        coverview.translatesAutoresizingMaskIntoConstraints = false
        houseview.translatesAutoresizingMaskIntoConstraints = false
        messagelabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
     
        
        //    设置约束
//       转轮
        self.addConstraint( NSLayoutConstraint(item: iconview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
//        遮罩
        self.addConstraint(NSLayoutConstraint(item: coverview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        
        
//        小房子
        self.addConstraint( NSLayoutConstraint(item: houseview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: houseview , attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
//        文本
        self.addConstraint(NSLayoutConstraint(item: messagelabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) )
        self.addConstraint(NSLayoutConstraint(item: messagelabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        self.addConstraint(NSLayoutConstraint(item: messagelabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
        
        //        注册
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//        宽高
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
//        登录
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//        宽高
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
    }
   
    
//   MARK :-按钮点击事件
    @objc private func registerButtonClick(){
//    调用代理方法
        Delegate?.registerClick()
        
    }

    @objc private func loginButtonClick(){
         Delegate?.loginClick()
    }
  
    
    
    
    
//    懒加载控件
//    zhuanlun
   private lazy var iconview: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

//    遮罩
    private lazy var coverview:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
//    小房子
    private lazy var houseview:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
//    消息label
   private lazy var messagelabel: UILabel = {
        let label = UILabel(frame: CGRectZero)
    
//        设置文字
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.numberOfLines = 0
        label.text = "关注一些人，看看有什么惊喜！"
//        让控件size根据内容自己调整
        label.sizeToFit()
        
    
      return label
    }()
    
//    注册按钮
   private lazy var registerButton: UIButton = {
       let button = UIButton(type: UIButtonType.Custom)
//        设置按钮属性
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
//    注册按钮的点击事件
       button .addTarget(self, action: Selector("registerButtonClick"), forControlEvents: UIControlEvents.TouchUpInside)
    
        return button
    }()
    
    
//    登录按钮
  private  lazy var loginButton: UIButton = {
    let button = UIButton(type: UIButtonType.Custom)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button .addTarget(self, action: Selector("loginButtonClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
    return button
    }()

}

























